package com.jerry.spring.security.jwt.util;

import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 *
 * @author jerry 2024/5/8 15:29
 */
@Component
public class RedisUtil {

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 缓存一个对象
     *
     * @param key
     * @param value
     * @param <T>
     */
    public <T> void setObject(final String key, final T value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 缓存一个对象（带过期时间）
     *
     * @param key
     * @param value
     * @param timeout
     * @param unit
     * @param <T>
     */
    public <T> void setObject(final String key, final T value, final long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    /**
     * 缓存List
     *
     * @param key
     * @param list
     * @param <T>
     * @return
     */
    public <T> Long setList(final String key, final List<T> list) {
        Long count = redisTemplate.opsForList().rightPushAll(key, list);
        return count == null ? 0 : count;
    }

    /**
     * 缓存Set
     *
     * @param key
     * @param set
     * @param <T>
     * @return
     */
    public <T> BoundSetOperations<String, T> setSet(final String key, final Set<T> set) {
        BoundSetOperations<String, T> operations = redisTemplate.boundSetOps(key);
        Iterator<T> iterator = set.iterator();
        while (iterator.hasNext()) {
            operations.add(iterator.next());
        }
        return operations;
    }

    /**
     * 缓存Map
     *
     * @param key
     * @param map
     * @param <T>
     */
    public <T> void setMap(final String key, final Map<String, T> map) {
        if (map != null) {
            redisTemplate.opsForHash().putAll(key, map);
        }
    }

    /**
     * 缓存Map中指定的key和value
     *
     * @param key
     * @param hashKey
     * @param value
     * @param <T>
     */
    public <T> void setMapValue(final String key, final String hashKey, final String value) {
        redisTemplate.opsForHash().put(key, hashKey, value);
    }

    /**
     * 获取缓存中的对象
     *
     * @param key
     * @param <T>
     * @return
     */
    public <T> T getObject(final String key) {
        ValueOperations<String, T> operations = redisTemplate.opsForValue();
        return operations.get(key);
    }

    /**
     * 获取缓存的List
     *
     * @param key
     * @param <T>
     * @return
     */
    public <T> List<T> getList(final String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }

    /**
     * 获取缓存的Set
     *
     * @param key
     * @param <T>
     * @return
     */
    public <T> Set<T> getSet(final String key) {
        return redisTemplate.opsForSet().members(key);
    }

    /**
     * 获取缓存的Map
     *
     * @param key
     * @param <T>
     * @return
     */
    public <T> Map<String, T> getMap(final String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 获取缓存Map中指定key的value
     *
     * @param key
     * @param hashKey
     * @param <T>
     * @return
     */
    public <T> T getMapValue(final String key, final String hashKey) {
        HashOperations<String, String, T> operations = redisTemplate.opsForHash();
        return operations.get(key, hashKey);
    }

    /**
     * 获取缓存Map中多个key的value
     *
     * @param key
     * @param hashKeys
     * @param <T>
     * @return
     */
    public <T> List<T> getMultiMapValue(final String key, final Collection<Object> hashKeys) {
        HashOperations<String, Object, T> operations = redisTemplate.opsForHash();
        return operations.multiGet(key, hashKeys);
    }

    /**
     * 获取缓存中所有的key
     *
     * @param patternPrefix 缓存key的前缀
     * @return
     */
    public Collection<String> keys(final String patternPrefix) {
        return redisTemplate.keys(patternPrefix);
    }

    /**
     * 删除缓存中指定key
     *
     * @param key
     * @return
     */
    public boolean delete(final String key) {
        return redisTemplate.delete(key);
    }

    /**
     * 删除缓存中的集合
     *
     * @param collection
     * @return
     */
    public long delete(final Collection collection) {
        return redisTemplate.delete(collection);
    }

    /**
     * 删除缓存中Map指定的key
     *
     * @param key
     * @param hashKey
     * @return
     */
    public long deleteMapValue(final String key, final String hashKey) {
        return redisTemplate.opsForHash().delete(key, hashKey);
    }

    /**
     * 设置指定key的过期时间
     *
     * @param key
     * @param timeout
     * @param unit
     * @param <T>
     */
    public <T> void expire(final String key, final long timeout, TimeUnit unit) {
        redisTemplate.expire(key, timeout, unit);
    }

}
